package top.infopub.mgr.mkt.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.dto.mkt.CommonCreditDetailsDto;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.dao.BusinessDetailsDao;
import top.infopub.mgr.mkt.dao.CreditDetailsDtoDao;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.mgr.mkt.dao.TechnicalDetailDtoDao;
import top.infopub.mgr.mkt.service.TechnicalDetailDtoService;
import top.infopub.model.mkt.*;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Warking
 * @Date 2021/5/8 14:36  技术标
 * @Version 1.0
 */
@Service
public class TechnicalDetailDtoServiceImpl extends GenericFlowController implements TechnicalDetailDtoService{
    //详情复用 资信标的
    @Autowired
    private CreditDetailsDtoServiceImpl creditDetailsDtoService;

    @Autowired
    private ProjectAttachmentDao projectAttachmentDao;
    @Autowired
    private CollectProjectServiceImpl collectProjectService;

    @Autowired
    private TechnicalDetailDtoDao technicalDetailDtoDao;

    @Autowired
    private CreditDetailsDtoDao creditDetailsDtoDao;

    @Override
    public CommonCreditDetailsDto queryDetail(String proCode) {
        //调用公用方法
        CommonCreditDetailsDto dto = creditDetailsDtoService.commonTender(new CommonCreditDetailsDto(), proCode,"3");

        List<TechnicalDetail> technicalDetailList = technicalDetailDtoDao.technicalDetail(proCode);
        dto.setTechnicalDetails(technicalDetailList);
        //附件信息表
        List<ProjectAttachment> projectAttaList = projectAttachmentDao.queryByWhere(proCode, ProjectFileTypeEnum.FILE_TYPE26.getCode(), null);
        dto.setFileAtt(collectProjectService.fileAttaListToArray(projectAttaList));
        return dto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int save(CommonCreditDetailsDto dto, String proCode) {
        //获取用户
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        //修改 标书评审的数据
        BidReview bidReview = dto.getBidReview();
        if (bidReview.getInvestEndTime() != null || bidReview.getWillApplyMeetTime() != null || (bidReview.getProManagers() != null && bidReview.getProManagers() != "")) {
            /*Review需要有项目全局唯一编码 */
            bidReview.setProCode(proCode);
            creditDetailsDtoDao.updateReview(bidReview);
        }
        //修改和保存技术标数据
        List<TechnicalDetail> list = dto.getTechnicalDetails();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                TechnicalDetail technicalDetail = list.get(i);
                if (technicalDetail.getId() != null && technicalDetail.getId() != "") {
                    //修改
                    technicalDetail.setUptBy(vo.getId());
                    technicalDetailDtoDao.updateSingle(technicalDetail);

                } else {
                    //保存
                    technicalDetail.setAddBy(vo.getId());
                    technicalDetail.setUptBy(vo.getId());
                    technicalDetail.setProCode(proCode);
                    technicalDetail.setBidEvalId(bidReview.getId());
                    technicalDetailDtoDao.insertSingle(technicalDetail);
                }
            }//for
        }// if


        //更新附件表-MKT_PROJECT_ATTACHMENT
        List<ProjectAttachment> attachmentList = new ArrayList<>();
        if (dto.getFileAtt() != null) {
            String[] fileAtt = dto.getFileAtt();
            for (String file : fileAtt) {
                ProjectAttachment attachment = createProjectFileAtta(vo, proCode, file);
                attachmentList.add(attachment);
            }
        }
        //更新附件信息
        projectAttachmentDao.deleteByWhere(proCode, ProjectFileTypeEnum.FILE_TYPE26.getCode(), null);
        if (attachmentList != null && attachmentList.size() > 0) {
            projectAttachmentDao.insertBatch(attachmentList);
        }
        return 1;
    }

    @Override
    public int deleteTechnicalDetail(String sguid) {
        return technicalDetailDtoDao.deleteBusinessDetail(sguid);
    }


}
